/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.customer.core.service.processor.register;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import info.peigen.hotpot.business.account.facade.order.create.CreateCustomerAccountOrder;
import info.peigen.hotpot.business.account.facade.result.create.CreateAccountResult;
import info.peigen.hotpot.business.customer.core.domain.Customer;
import info.peigen.hotpot.business.customer.core.service.processor.base.AbstractCustomerServiceProcessor;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.service.base.exception.BusinessException;
import info.peigen.hotpot.common.service.base.processor.Processor;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import org.noear.solon.annotation.Component;

/**
 * <b>(CustomerCreateAccountProcessor)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2022/5/26
 */
@SuppressWarnings("rawtypes")
@Component
public class CustomerCreateAccountProcessor extends AbstractCustomerServiceProcessor implements Processor {
    @Override
    public Integer level() {
        return 60;
    }

    @Override
    public void execute(ProcessorServiceContext serviceContext) {
        Customer customer = (Customer) serviceContext.getDomain();
        CreateCustomerAccountOrder order = CreateCustomerAccountOrder.builder()
                .customerId(customer.getCustomerId())
                .partnerId(customer.getPartnerId()).build();
        CreateAccountResult result = accountServiceFacade.createCustomerAccount(order);
        Assert.isTrue(ObjectUtil.isNotNull(result) && result.success(), () -> new BusinessException(ResultCode.FAILURE,
                "创建[客户]账户失败![openId=" + customer.getCustomerTech().getWxMiniappUser().getOpenid() +
                        ",nickname=" + customer.getCustomerTech().getWxMiniappUser().getNickName() + "]"));
    }
}
